#include "head.h"

void initPathStack(PathStack *stack, char *user) {
    //初始化原始路径
    char path[1024] = { 0 };
    strcpy(path, " /home/");
    strcat(path, user);
    strcat(path, "/");
    strcat(stack->init_path, path);
    printf("初始路径：%s\n", stack->init_path);

    stack->top = 0;
    //初始化命令前缀
    strncpy(stack->init_order, "sudo -u ",20);
    strcat(stack->init_order, user);

    strncpy(stack->paths[stack->top], user, MAX_PATH_LEN);
}

int pathPush(PathStack *stack, const char *path) {
    if (stack->top >= STACK_SIZE - 1) {
        return -1; 
    }
    char buf[1024] = { 0 };
    strcpy(buf, path);
    strcat(buf, "/");
    stack->top ++;
    strncpy(stack->paths[stack->top], buf, MAX_PATH_LEN);
    return 0;
}

int pathPop(PathStack *stack, char *path) {
    if (stack->top == 0) {
        return -1; 
    }
    strncpy(path, stack->paths[stack->top], MAX_PATH_LEN);
    stack->top --;
    return 0;
}

int pathPeek(const PathStack *stack, char *path) {
    if (stack->top == -1) {
        return -1;
    }
    strncpy(path, stack->paths[stack->top], MAX_PATH_LEN);
    return 0;
}

//处理PWD函数
int client_pwd(PathStack *stack , int net_fd){
    //用来存储当前目录
    char cur_path[1024] = { 0 };
    bzero(cur_path, sizeof(cur_path));
    pathPeek(stack, cur_path);

    printf("当前用户虚拟路径：%s\n", cur_path);

    //发送当前路径
    int num = send(net_fd, cur_path, strlen(cur_path), MSG_NOSIGNAL);

    printf("pwd 发送字节数: %d\n", num);
    return 0;
}

// ls ll 两个命令 
int client_check_order(PathStack *stack, int net_fd, char *order){
    //组合路径
    char vir_path[1024] = { 0 }; 
    pathPeek(stack, vir_path);
    char cur_path[1024] = { 0 };
    strncpy(cur_path, stack->init_path, 1024);
    strcat(cur_path, vir_path);
    printf("真实路径 ：%s\n", cur_path);

    char new_order[1024] = { 0 };
    if(strcmp(order, "ll") == 0){
        strcat(new_order, "ls -l ");
        strcat(new_order, cur_path);
        strcat(new_order, " > order.txt");
        printf("check order : %s\n", new_order);
        system(new_order);
    }else {
        strcat(new_order, order);
        strcat(new_order, cur_path);
        strcat(new_order, " > order.txt");
        printf("check order : %s\n", new_order);
        system(new_order);
    }

    int fd = open("order.txt", O_RDWR | O_CREAT, 0666); 
    char buf[1024] = { 0 };
    bzero(buf, sizeof(buf));
    int res_read = read(fd, buf, sizeof(buf));
    if (res_read == 0){
        send(net_fd, "\n", strlen("\n"), MSG_NOSIGNAL);
    }else{
        int ls_num = send(net_fd, buf, strlen(buf), MSG_NOSIGNAL);
        ERROR_CHECK(ls_num, -1, "send");
    }
    system("rm -r order.txt");
    return 0;
}

//文件操作命令:remove
int client_remove(PathStack *stack, int net_fd, char *order){
    //组合路径
    char vir_path[1024] = { 0 };  
    pathPeek(stack, vir_path);
    char cur_path[1024] = { 0 };
    strncpy(cur_path, stack->init_path, 1024);
    strcat(cur_path, vir_path);

    char new_order[1024] = { 0 };
    strcat(new_order, " rm -r ");
    strcat(new_order, cur_path);
    strcat(new_order, "/");
    strcat(new_order, order);
    int res = system(new_order);
    if(res != 0){
        send(net_fd, "delete fail!", strlen("delete fail!"), MSG_NOSIGNAL);
    }
    else{
        send(net_fd, "delete sucess!", strlen("delete sucess!"), MSG_NOSIGNAL);
    }
    return 0;
}


int client_mkdir(PathStack *stack, int net_fd, char *order){
    printf("order_init : %s\n", order);
    //组合路径
    char vir_path[1024] = { 0 };  
    pathPeek(stack, vir_path);
    char cur_path[1024] = { 0 };
    strncpy(cur_path, stack->init_path, 1024);
    strcat(cur_path, vir_path);
    printf("=========\n");
    char new_order[1024] = { 0 };
    
    strcat(new_order, "mkdir ");
    printf("order1 : %s\n", new_order);
    strcat(new_order, cur_path);
    printf("order2 : %s\n", new_order);
    strcat(new_order, "/");
    strcat(new_order, order);
    printf("order3 : %s\n", new_order);
    int res = system(new_order);
    if(res != 0){
        send(net_fd, "mkdir fail!", strlen("mkdir fail!"), MSG_NOSIGNAL);
    }
    else{
        send(net_fd, "mkdir sucess!", strlen("mkdir sucess!"), MSG_NOSIGNAL);
    }
    return 0;
}

int path_cd (PathStack *stack, int net_fd, char *order){
    char vir_path[1024] = {0};
    pathPeek(stack,vir_path);//使vir_path里面存栈顶元素
    printf("vir_path : %s\n", vir_path);
    char cur_path[1024] = {0};
    strncpy(cur_path,stack->init_path,1024);//拼接栈顶元素到初始目录后
    strcat(cur_path, vir_path);
    printf("cur_path : %s\n", cur_path);
    char test_cd[1024] = {0};
    
    strcat(test_cd,"cd ");
    char tar_path[1024] = {0};
    strcpy(tar_path,cur_path);
    strcat(tar_path,"/");
    strcat(tar_path,order);
    strcat(test_cd,tar_path);

    if(strcmp(order,"..") == 0){
        pathPop(stack,tar_path);
    }
    else{
        int ret = system(test_cd);
        if(ret != 0){
            send(net_fd, "cd fail!", strlen("cd fail!"),MSG_NOSIGNAL);
        } 
        else{
            pathPush(stack,tar_path);
        }
    }
    return 0;
}

